package com.woniu.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.ExceptionCode;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.token.AccToken;
import com.ruoyi.common.utils.token.JwtTokenUtils;
import com.woniu.user.domain.Customer;
import com.woniu.user.domain.RegisterCustomer;
import com.woniu.user.service.ICustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 外部用户信息Controller
 * 
 * @author yy
 * @date 2023-04-03
 */
@RestController
@RequestMapping("/user/tbcustomer")
@Api("外部用户信息管理")
public class CustomerController extends BaseController {
    @Autowired
    private ICustomerService customerService;

    @RequestMapping("webLogin")
    public Customer selectCustomerByAccountAndPass(@RequestParam("customerAccount") String customerAccount,@RequestParam("customerPassword") String customerPassword){
        Customer customer = customerService.selectCustomerByAccountAndPass(customerAccount,customerPassword);
        return customer;
    }

    @RequestMapping("queryWebCode")
    public AjaxResult queryWebCode(@RequestParam("phone") String phone){
        String code = customerService.queryWebCode(phone);
        return AjaxResult.success("验证码已发送!"+code);
    }

    @RequestMapping("webRegister")
    public AjaxResult webRegister(@RequestBody RegisterCustomer registerCustomer){
        System.out.println("register ---> "+ registerCustomer);
        customerService.webRegister(registerCustomer);
        return AjaxResult.success("注册成功!");
    }

    //锁定该用户
    @RequestMapping("lockTbCustomer")
    public AjaxResult lockTbCustomer(@RequestParam("customerId") Integer customerId){
        System.out.println("要锁定的用户id ---> "+customerId);
        customerService.lockTbCustomer(customerId);
        return AjaxResult.success("锁定成功!");
    }

    //逻辑删除
    @RequestMapping("lockTbCustomerTwo")
    public AjaxResult lockTbCustomerTwo(@RequestParam("customerId") Integer customerId){
        System.out.println("要删除的用户id ---> "+customerId);
        customerService.lockTbCustomerTwo(customerId);
        return AjaxResult.success("删除成功!");
    }

    //恢复用户权限
    @RequestMapping("resumeCustomer")
    public AjaxResult resumeCustomer(@RequestParam("customerId") Integer customerId){
        System.out.println("要恢复的用户id ---> "+customerId);
        customerService.resumeCustomer(customerId);
        return AjaxResult.success("恢复成功!");
    }

    @RequestMapping("checkWebUserName")
    public AjaxResult checkWebUserName(@RequestParam("userJwt") String jwt){
        System.out.println("checkWebUserName --> " + jwt);

        if (jwt != null){
            AccToken accToken = JwtTokenUtils.parseWebToken(jwt);
            Long userId = accToken.getUserId();
            Customer customer = customerService.selectCustomerByCustomerId(userId);
            return AjaxResult.success("ok",customer);
        }
        return AjaxResult.warn("您还没有登录!");
    }

    @RequestMapping("initWebUserInfo")
    public AjaxResult initWebUserInfo(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userId = request.getHeader("userId");
        System.out.println("userId --> "+userId);
        //Integer customerId = Integer.parseInt(userId);
        Customer customer = customerService.selectCustomerByCustomerId(Long.valueOf(userId));
        System.out.println("initWebUserInfo ==> "+customer);
        return AjaxResult.success("ok",customer);
    }


    @RequestMapping("uploadWebUser")
    public AjaxResult uploadWebUser(@RequestBody Customer customer){
        System.out.println("uploadCus ---> "+ customer);
        int i = customerService.uploadWebUser(customer);
        if (i > 0){
            return AjaxResult.success("修改成功!");
        }else {
            return AjaxResult.error("修改失败!");
        }
    }

    /**
     * 查询外部用户信息列表
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomer:list')")
    @GetMapping("/list")
    @ApiOperation("外部用户信息分页查询")
    public TableDataInfo list(Customer customer)
    {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 导出外部用户信息列表
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomer:export')")
    @Log(title = "外部用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("外部用户信息导出")
    public void export(HttpServletResponse response, Customer customer)
    {
        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.exportExcel(response, list, "外部用户信息数据");
    }

    /**
     * 获取外部用户信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomer:query')")
    @GetMapping(value = "/{customerId}")
    @ApiOperation("根据主键查询外部用户信息")
    @ApiImplicitParam(name = "customerIds", value = "用户ID", required = true, dataType = "Long")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId)
    {
        return success(customerService.selectCustomerByCustomerId(customerId));
    }

    @RequestMapping("feign/getCustomerById")
    public Customer getCustomerById(@RequestParam("customerId") Long customerId)
    {
      return customerService.selectCustomerByCustomerId(customerId);
    }


    /**
     * 新增外部用户信息
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomer:add')")
    @Log(title = "外部用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加外部用户信息")
    public AjaxResult add(@RequestBody Customer customer)
    {
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改外部用户信息
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomer:edit')")
    @Log(title = "外部用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("更新外部用户信息")
    public AjaxResult edit(@RequestBody Customer customer)
    {
        return toAjax(customerService.updateCustomer(customer));
    }



    @RequestMapping("/feign/edit")
    public Integer editCustomer(@RequestBody Customer customer)
    {
        return customerService.updateCustomer(customer);
    }

    /**
     * 删除外部用户信息
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomer:remove')")
    @Log(title = "外部用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    @ApiOperation("删除外部用户信息")
    @ApiImplicitParam(name = "customerIds", value = "主键数组", required = true, dataType = "Long")
    public AjaxResult remove(@PathVariable Long[] customerIds)
    {
        return toAjax(customerService.deleteCustomerByCustomerIds(customerIds));
    }


}
